package com.fenghuajueli.module_user.network.repository

import com.blankj.utilcode.util.LogUtils
import com.fenghuajueli.libbasecoreui.baseswitch.SwitchKeyUtils
import com.fenghuajueli.module_user.network.result.TemplateResult
import com.fenghuajueli.module_user.network.retrofit.BackgroundNetwork
import kotlinx.coroutines.flow.MutableSharedFlow

/**
 * @Author: sz_ijj
 * @Date: 2024-03-21 14:30
 **/
object TemplateRepository {
    @JvmStatic
    var initializerState = false

    @JvmStatic
    val templateData: MutableSharedFlow<TemplateResult> =
        MutableSharedFlow(replay = 1)

    suspend fun initTemplateData() {
        runCatching {
            val templateType = SwitchKeyUtils.getVipTemplateSwitch().pay_template
            val result = BackgroundNetwork.getTemplate(templateType)
            LogUtils.d("付费模板数据:${result}")

            when (result.code) {
                10000 -> {
                    initializerState = true
                    TemplateResult.Success(result.data)
                }

                else -> {
                    TemplateResult.Default(result.msg)
                }
            }
        }.onFailure {
            initializerState = false
            templateData.emit(TemplateResult.Default())
            LogUtils.e(it.message)
        }.onSuccess {
            templateData.emit(it)
        }
    }
}